﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;

namespace YinLongTool.Net
{
	public class UrlHelper
	{
		public static List<KeyValuePair<string, string>> GetParas(string url)
		{
			List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();
			Uri uri = new Uri(url);
			if (url.Contains("?"))
			{
				int num = url.IndexOf('?') + 1;
				string text = url.Substring(num, url.Length - num);
				string[] array = text.Split('&');
				string[] array2 = array;
				foreach (string text2 in array2)
				{
					if (!string.IsNullOrEmpty(text2.Trim()))
					{
						int num2 = text2.IndexOf("=");
						if (num2 > -1)
						{
							string key = text2.Substring(0, num2);
							string value = text2.Substring(num2 + 1, text2.Length - num2 - 1);
							list.Add(new KeyValuePair<string, string>(key, value));
						}
						else
						{
							list.Add(new KeyValuePair<string, string>(text2, ""));
						}
					}
				}
			}
			return list;
		}

		public static string GetParaValue(string url, string key)
		{
			List<KeyValuePair<string, string>> paras = GetParas(url);
			foreach (KeyValuePair<string, string> item in paras)
			{
				if (item.Key.ToLower() == key.ToLower())
				{
					return item.Value;
				}
			}
			return "";
		}

		public static string RemoveQuery(string url)
		{
			if (url.Contains("?"))
			{
				return url.Split('?')[0];
			}
			return url;
		}

		public static string RemovePara(string url, params string[] paraNames)
		{
			string text = RemoveQuery(url);
			List<KeyValuePair<string, string>> paras = GetParas(url);
			if (paras.Count > 0)
			{
				text += "?";
				int num = 0;
				foreach (KeyValuePair<string, string> item in paras)
				{
					if (Array.IndexOf(paraNames, item.Key) <= -1)
					{
						if (num++ > 0)
						{
							text += "&";
						}
						text += $"{item.Key}={item.Value}";
					}
				}
			}
			return text;
		}

		public static List<string> GetRedirectList(string url, int timeOut)
		{
			List<string> list = new List<string>();
			list.Add(url);
			HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
			httpWebRequest.Timeout = timeOut;
			httpWebRequest.AllowAutoRedirect = false;
			HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
			httpWebResponse.Close();
			int num = 0;
			while ((httpWebResponse.StatusCode == HttpStatusCode.Found || httpWebResponse.StatusCode == HttpStatusCode.MovedPermanently || httpWebResponse.StatusCode == HttpStatusCode.Found) && num++ <= 5)
			{
				string text = httpWebResponse.Headers[HttpResponseHeader.Location];
				if (!Uri.TryCreate(text, UriKind.Absolute, out Uri _))
				{
					break;
				}
				list.Add(text);
				httpWebRequest = (HttpWebRequest)WebRequest.Create(text);
				httpWebRequest.Timeout = timeOut;
				httpWebRequest.AllowAutoRedirect = false;
				httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
				httpWebResponse.Close();
			}
			return list;
		}

		public static string ReplaceParaValue(string url, string para, string value)
		{
			string str = RemoveQuery(url);
			List<KeyValuePair<string, string>> paras = GetParas(url);
			if (paras.Count > 0)
			{
				str += "?";
				int num = 0;
				bool flag = false;
				foreach (KeyValuePair<string, string> item in paras)
				{
					if (num++ > 0)
					{
						str += "&";
					}
					if (item.Key.ToLower() == para.ToLower())
					{
						str += $"{item.Key}={value}";
						flag = true;
					}
					else
					{
						str += $"{item.Key}={item.Value}";
					}
				}
				if (!flag)
				{
					if (num > 0)
					{
						str += "&";
					}
					str += $"{para}={value}";
				}
			}
			else
			{
				str += $"?{para}={value}";
			}
			return str;
		}

		public static string AddPara(string url, string para, string value)
		{
			List<KeyValuePair<string, string>> paras = GetParas(url);
			paras.Add(new KeyValuePair<string, string>(para, value));
			string text = RemoveQuery(url);
			if (paras.Count > 0)
			{
				text += "?";
				int num = 0;
				foreach (KeyValuePair<string, string> item in paras)
				{
					if (num++ > 0)
					{
						text += "&";
					}
					text = ((!string.IsNullOrEmpty(item.Key)) ? (text + $"{item.Key}={item.Value}") : (text + $"{item.Value}"));
				}
			}
			return text;
		}
	}
}
